package ws.very.web.tpl.nutz

import java.io.File
import ws.very.web.tpl.ScalaTemplateCompiler

object TplMonitor extends App {

  private lazy val FileRegex = """([a-zA-Z0-9_]+)[.]ws[.]([a-z]+)""".r

  private lazy val resultType = "Html"
  private lazy val formatterType = "HtmlFormat"

  def apply(sourceDirectory: File, generatedDirectory: File, otherFilter: (File => Boolean) = { f => true }, otherImports: String = "", waitTime: Int = 100) = {

    def compiler(file: File): Unit =
      (file.isDirectory, file.getName) match {
        case (true, _) => file.listFiles.foreach { compiler }
        case (false, FileRegex(name, typ)) if (otherFilter(file)) =>
          ScalaTemplateCompiler.compile(file, sourceDirectory, generatedDirectory, resultType, formatterType, otherImports)
        case _ =>
      }
    while (true) {
      compiler(sourceDirectory)
      synchronized {
        this.wait(waitTime)
      }
    }
  }

}